<template>
  <div class="app-container" style="background: #f3f4f6;flex: 1;padding: 24px;box-sizing: border-box;overflow: hidden;">
    <div class="teacher_info_wrap">
      <div class="teacher_info_form">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="left">
          <el-row>
            <el-col :span="12">
              <el-form-item label="教师姓名" prop="teacherName" style="margin-right: 20px;">
                <el-input v-model="form.teacherName" placeholder="请输入教师姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="教师工号" prop="teacherNo"  style="margin-left: 20px;">
                <el-input v-model="form.teacherNo" placeholder="请输入教师工号" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="性别" prop="gender" style="margin-right: 20px;">
                <el-select v-model="form.gender" placeholder="请选择性别">
                  <el-option
                    v-for="dict in dict.type.teacher_sex"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系方式" prop="teacherPhone" style="margin-left: 20px;">
                <el-input v-model="form.teacherPhone" placeholder="请输入联系方式" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="电子邮箱" prop="teacherEmail" style="margin-right: 20px;">
                <el-input v-model="form.teacherEmail" placeholder="请输入邮箱" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="职称/职务" prop="title" style="margin-left: 20px;">
                <el-select v-model="form.title" placeholder="请选择职称/职务">
                  <el-option
                    v-for="dict in dict.type.teacher_title"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="状态" prop="status" style="margin-right: 20px;">
                <el-radio-group v-model="form.status">
                  <el-radio
                    v-for="dict in dict.type.classai_common_status"
                    :key="dict.value"
                    :label="parseInt(dict.value)"
                  >{{dict.label}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
<!--            <el-col :span="12">-->
<!--              <el-form-item label="初始密码" prop="teacherPassword" style="margin-left: 20px;">-->
<!--                <el-input v-model="form.teacherPassword" placeholder="请输入6-16位密码" />-->
<!--              </el-form-item>-->
<!--            </el-col>-->
          </el-row>
          <el-form-item label="教师照片" prop="photoPath">
            <image-upload v-model="form.photoPath"/>
          </el-form-item>
          <el-form-item label="个人简介" prop="description">
            <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-form>
      </div>
      <div class="teacher_info_btn">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { addTeacher } from "@/api/classai/teacher";

export default {
  name: "AddTeacher",
  dicts: ['classai_common_status', 'teacher_sex', 'teacher_title'],
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        teacherNo: [
          { required: true, message: "教师工号不能为空", trigger: "blur" }
        ],
        teacherName: [
          { required: true, message: "教师姓名不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "请选择账号状态", trigger: "blur" }
        ],
        teacherPassword: [
          { required: true, message: "初始密码不能为空", trigger: "blur" },
          {
            pattern: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,16}$/,
            message: '密码需包含字母和数字，长度为6-16位',
            trigger: 'blur'
          }
        ],
      }
    };
  },
  methods: {
    // 取消按钮
    cancel() {
      this.reset();
      this.$router.back()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teacherNo: null,
        teacherName: null,
        gender: null,
        title: null,
        photoPath: null,
        description: null,
        teacherPhone: null,
        teacherEmail: null,
        createTime: null,
        updateTime: null,
        status: null,
        password: null,
        loginDate: null,
        loginIp: null,
        lastLoginDate: null,
        lastLoginIp: null,
        schoolId: null
      };
      this.resetForm("form");
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // this.form.teacherPassword = '123456'
          addTeacher(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.cancel();
          });
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
  .teacher_info_wrap {
    width: 100%;
    height: 100%;
    overflow: hidden;
    border-radius: 6px;
    background: #fff;
    box-sizing: border-box;
    border: 1px solid rgba(0, 0, 0, 0);
    box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.05);
    padding: 24px;
    .teacher_info_form {
      height: calc(100% - 60px);
      overflow-y: auto;
      ::v-deep {
        .el-form-item {
          display: flex;
          flex-direction: column;
        }
        .el-form-item__content {
          margin-left: 0!important;
          .el-select {
            width: 100%;
          }
        }
      }
    }
    .teacher_info_btn {
      display: flex;
      align-items: center;
      justify-content: flex-end;
      height: 60px;
    }
  }


</style>
